'use strict';

const Translator = require('../core/translater');

// const apiUrl = 'https://fanyi-api.baidu.com/api/trans/vip/translate';
// const appid = '20220507001206583';
// const secret = 'Jwxa9esymmZe2E6vMbq0';

// const appid = '20220512001213836';
// const secret = '76Q3ynW2hgJZLzvIqOvy';

// const requestTranslate = q => {
//   const salt = Math.random();
//   const sign = crypto.createHash('md5').update(appid + q + salt + secret).digest('hex');
//   const params = {
//     q,
//     from: 'zh',
//     to: 'en',
//     salt,
//     appid,
//     sign,
//   };
//   return axios.get(apiUrl, {
//     params,
//   });
// };

// 获取Service类
const Service = require('egg').Service;

class TranslaterService extends Service {
  async translate(qs) {
    const translator = new Translator();

    // config the translator
    translator.config = {
      from: 'zh_CHS', // zh-CHS(中文) || ja(日语) || EN(英文) || fr(法语) ...
      to: 'EN',
      appKey: '73648bc3b8bb68b5', // https://ai.youdao.com 在有道云上进行注册
      secretKey: 'lQnF2xU6skNCwvAHW2GQg6fatOTFeIka',
    };

    const resultStr = await translator.translate(qs);
    console.log(JSON.parse(resultStr).translation[0]);

    return JSON.parse(resultStr).translation[0];

  }
}

module.exports = TranslaterService;
